from datetime import datetime, timedelta
from feature_conf.config import GenericConfigConstant


def get_local_time(trans_time=datetime.now(), country_id='mx'):
    """

    Args:
        trans_time:
        country_id:

    Returns:

    """
    if country_id not in GenericConfigConstant.COUNTRY_ID:
        raise ValueError("country id not in list, Please input correct country id")
    tz = GenericConfigConstant.COUNTRY_TIME_ZONE[country_id]
    right_time = trans_time + timedelta(hours=tz)
    return right_time


def time_trans(time, country_code):
    tz = GenericConfigConstant.COUNTRY_TIME_ZONE[country_code.lower()]
    time_len = len(str(time))
    time_format = "%a %b %d %H:%M:%S GMT%z %Y"
    if time_len == 10:
        format_time = datetime.utcfromtimestamp(int(time)) + timedelta(hours=tz)
    elif time_len == 13:
        format_time = datetime.utcfromtimestamp(int(time) // 1000) + timedelta(hours=tz)
    elif time_len == 34:
        format_time = datetime.strptime(str(time), time_format)
    else:
        format_time = datetime(2099, 12, 31, 23, 59, 59)

    return format_time
